import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STColumn, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFSelectWidgetSchema, SFUISchema } from '@delon/form';
import { ModalHelper } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BannerService } from '../../services/banner.service';

@Component({
  selector: 'app-banner-components-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.less']
})
export class BannerComponentsListComponent implements OnInit {
  schema: SFSchema = {};
  columns: STColumn[] = [];
  ui: SFUISchema = {};
  appList: any[] = [];
  _$expand = false;
  selectApp = {
    appName: '',
    appId: ''
  };
  isLoading: boolean = false;

  @ViewChild('st', { static: false }) st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  constructor(
    public service: BannerService,
    private modal: ModalHelper,
    private msg: NzMessageService,
    private router: Router,
    private modalSrv: NzModalService,
    private ar: ActivatedRoute
  ) {}

  /**
   * 查询字段个数
   */
  get queryFieldCount(): number {
    return Object.keys(this.schema?.properties || {}).length;
  }

  /**
   * 查询参数
   */
  get reqParams() {
    const params = Object.assign({}, this.sf?.value || {});
    delete params._$expand;
    if (params.status === '') {
      delete params.status;
    }
    if (params.style === '') {
      delete params.style;
    }
    if (params.navigationId === '') {
      delete params.navigationId;
    }
    return { ...params };
  }

  /**
   * 选中行
   */
  get selectedRows() {
    return this.st?.list.filter(item => item.checked) || [];
  }
  ngOnInit() {
    this.initSF();
    this.initST();
  }
  selectAppFun(item: any) {
    this.selectApp = item;
    this.st.load(1);
  }
  dataProcess(data: STData[]): STData[] {
    return data.map((i, index) => {
      i.showSortFlag = false;
      return i;
    });
  }
  initSF() {
    this.schema = {
      properties: {
        _$expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        name: {
          type: 'string',
          title: 'banner名称',
          maxLength: 10,
          ui: {
            widget: '',
            placeholder: '请输入'
          }
        },
        status: {
          type: 'string',
          title: '状态',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true
          } as SFSelectWidgetSchema,
          enum: [
            { label: '全部', value: 1 },
            { label: '正常', value: 2 },
            { label: '禁用', value: 3 }
          ]
        }
      }
    };
    this.ui = {
      '*': {
        spanLabelFixed: 110,
        grid: { span: 8 }
      }
    };
  }

  initST() {
    this.columns = [
      {
        title: 'banner名称', // 位:px
        index: 'name',
        className: 'text-center'
      },
      {
        title: 'banner',
        index: 'navigationName',
        className: 'text-center'
      },
      {
        title: '排序', // 位 px
        index: 'sortId',
        className: 'text-center'
      },
      {
        title: '状态', // 位 px
        index: 'style',
        className: 'text-center',
        type: 'enum',
        enum: {
          1: '正常',
          2: '禁用'
        }
      },
      {
        title: '最后修改时间', // 位 px
        index: 'createTime',
        className: 'text-center'
      },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 280,
        buttons: [
          {
            text: '修改',
            acl: { ability: ['banner-change'] },
            click: item => {
              this.router.navigate(['../detail'], { queryParams: { id: item.id, type: 'edit' }, relativeTo: this.ar });
            }
          },
          {
            text: '禁用',
            acl: { ability: ['banner-forbidden'] },
            pop: {
              title: `确定禁用此banner图吗？？`,
              okType: 'danger',
              icon: 'alert'
            },
            click: item => {
              this.changeStatus(item.id);
            },
            iif: item => item.status === 1
          },
          {
            text: '启用',
            acl: { ability: ['banner-startUseing'] },
            pop: {
              title: `确定启用此banner图吗？`,
              okType: 'danger',
              icon: 'success'
            },
            click: item => {
              this.changeStatus(item);
            },
            iif: item => item.status === 2
          }
        ]
      }
    ];
  }
  changeStatus(item: any) {
    const params = {
      status,
      idList: [item.id]
    };
    // this.service.request(this.service.$api_openOrClose, params).subscribe(res => {
    //   if (res) {
    //     this.st.reload();
    //   }
    // });
  }
  /**
   * 伸缩查询条件
   */
  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/_$expand', this._$expand);
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
    this.isLoading = true;
  }

  /**
   * 新增单个实例
   */
  add() {
    this.router.navigate(['../banner/detail'], { queryParams: { id: 0, type: 'add' }, relativeTo: this.ar });
  }

  /**
   * 删除单个实例
   */
  del(item: any) {
    const ids = [];
    ids.push(item.id);
    this.service.request(this.service.$api_del_many, ids).subscribe(res => {
      if (res) {
        this.service.msgSrv.success('删除成功');
        this.st.reload();
      }
    });
  }
}
